<template>
  <div>
    <router-link tag='div' to='/' v-show='showAbs'
      class="header-abs" :style='opacityStyleAbs'
    >
      <span class="iconfont header-abs-back">&#xe624;</span>
    </router-link>
    <div class="header-fixed" v-show='showFixed' :style='opacityStyle'>
      <router-link to='/'>
        <div class="iconfont header-fixed-back">&#xe624;</div>
      </router-link>
      景点详情
    </div>
  </div>
</template>
<script>
export default {
  name: 'DetailHeader',
  data () {
    return {
      showAbs: true,
      showFixed: false,
      opacityStyle: {
        opacity: 0
      },
      opacityStyleAbs: {
        opacity: 1
      }
    }
  },
  methods: {
    handleScroll (e) {
      let scrollTop = document.documentElement.scrollTop
      if (scrollTop > 30) {
        this.showAbs = false
        this.showFixed = true
        const opacity = scrollTop / 100
        this.opacityStyle.opacity = opacity > 1 ? 1 : opacity
      } else {
        this.showAbs = true
        this.showFixed = false
      }
      // if (scrollTop < 100) {
      //   this.showAbs = true
      //   const opacityAbs = 1 - (scrollTop / 100)
      //   this.opacityStyleAbs.opacity = opacityAbs
      // } else {
      //   this.showAbs = false
      // }
    }
  },
  activated () {
    window.addEventListener('scroll', this.handleScroll)
  },
  deactivated () {
    // 全局事件一定要解绑，不然在每个页面都要执行
    window.removeEventListener('scroll', this.handleScroll)
  }
}
</script>
<style lang='stylus' scoped>
@import '~styles/varibles.styl'
.header-abs
  position: fixed
  left: .1rem
  top: .1rem
  width: .72rem
  height: .72rem
  border-radius: .36rem
  background: rgba(0, 0, 0, .6)
  .header-abs-back
    color: #ffffff
    font-size: .36rem
    line-height: .72rem
.header-fixed
  position: fixed
  top: 0
  left: 0
  right: 0
  overflow: hidden
  height: $headerHeight
  line-height: .88rem
  color: #fff
  background: $bgColor
  font-size: .32rem
  z-index: 2
  .header-fixed-back
    position: absolute
    width: .88rem
    top: 0
    left: 0
    color: #fff
</style>
